`JAVA中FileInputStream 和FileOutputStream方法的问题....

来源:百度知道 编辑:UC知道 时间:2024/06/16 22:25:36
import java.io.*;
class FileCopy
{
public static void main (String[] args) {
FileInputStream in;
FileOutputStream out;
try
{
in=new FileInputStream(args[0]);
out =new FileOutputStream(args[1]);
copyFile(in,out);
}catch(IOException e)
{
System.out.println("Error:"+e);
System.exit(-3);

}
}
static void copyFile(FileInputStream in, FileOutputStream out)
{
int length;
byte buf[]=new byte[1024];
try{
while((length=in.read(buf,0,1024))!=-1)
{
out.write(buf,0,1024);
}
}catch(IOException e)
{
System.out.println("Error:"+e);
System.exit(-4);

}

}
}

书上的一个例题...貌似这个例题有问题..编译通过`结果出问题``看不懂..
例题的原意是 让 copyFile()方法实现文件拷贝功能...
如果要达到例题的用意..该怎么编..?

代码没有问题,完成了拷贝的功能。

是你在运行的时候没有输入此参数,所以报错了。
错误信息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at cn.com.storm.common.test.FileCopy.main(FileCopy.java:12)

输入命令:java FileCopy c:\a.txt d:\b.txt
这样就可一把C盘下的a.txt拷贝到D盘下的b.txt了。
试试吧!

代码应该没有错误的!我的JDK不能用了,没有办法调试了,
你可能是这样的,你的arg[0],arg[1]这些参数是不是在命令行下输入了呢?肯定要接受参数的啊!要不怎么运行啊?